package com.elvt.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elvt.server.mapper.DepartmentMapper;
import com.elvt.server.pojo.Department;
import com.elvt.server.service.IDepartmentService;
import com.elvt.server.utils.RespSult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 作者：elvt
 * @since 2022-05-21
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    /**
     * 获取所有部门
     * @return
     */
    @Override
    public List<Department> getAllDepartments() {

        return departmentMapper.getAllDepartments(-1);
    }
    /**
     * 添加部门
     * @param department 数据
     * @return
     */
    @Override
    public RespSult addDepartment(Department department) {
        department.setEnabled(true);
        departmentMapper.addDepartment(department);
        if (department.getResult()==1){
            return RespSult.success("添加成功",department);
        }
        return RespSult.error("添加失败") ;

    }
    /**
     * 根据id删除部门
     * @param id 条件
     * @return
     */
    @Override
    public RespSult deleteDepById(Integer id) {
        Department dep=new Department();
        dep.setId(id);
        departmentMapper.deleteDepById(dep);
        if (-2==dep.getResult()){
            return RespSult.error("该部门下还有子部门，删除失败！");
        }
        if (-1==dep.getResult()){
            return RespSult.error("该部门下还有员工，删除失败！");
        }
        if (1==dep.getResult()){
            return RespSult.success("删除成功！");
        }
        return RespSult.error("删除失败！");
    }
}
